import {
  getCategoryList
} from '../../../services/good/fetchCategoryList';
import {
  getWorkDetail
} from '../../../api/index';
Page({
  data: {
    list: [],
    id: '',
    address: '',
    pageNum: 1, // 当前页码  
    pageSize: 12, // 每页显示的条目数  
    isLoading: false, // 是否正在加载数据  
    hasMore: true, // 是否有更多数据可以加载 
    worklist: [],
    baseUrl: 'https://oss.peibanmao.com/'

  },
  init() {
    // try {
    //   const result = await getCategoryList();
    //   this.setData({
    //     list: result,
    //   });
    // } catch (error) {
    //   console.error('err:', error);
    // }
  },

  onShow() {
    //this.getTabBar().init();
    this.getTabBar()

  },
  onChange() {
    // wx.navigateTo({
    //   url: '/pages/goods/list/index',
    // });
  },
  onLoad(options) {
    this.init(true);
    console.log(options, 'options')
    this.setData({
      id: options.id
    })
    this.setData({
      address: options.itemAddress
    })
    this.getWorkDetailFn()
  },

  getWorkDetailFn() {
    if (this.data.isLoading || !this.data.hasMore) {
      return;
    }
    this.setData({
      isLoading: true,
    });
    getWorkDetail({
      id: Number(this.data.id),
    }).then(res => {
      const newData = this.data.worklist.concat(res.data.item);
      const hasMore = res.hasMore;
      this.setData({
        worklist: newData,
        pageNum: this.data.pageNum + 1,
        isLoading: false,
        hasMore: hasMore,
      });
    }).catch(error => {
      console.error('获取工作列表失败:', error);
      this.setData({
        isLoading: false,
      });
    });
  },
  onReachBottom() {
    if (this.data.hasMore) {
      this.getWorklist();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
      });
    }
  },
  onChange(e) {
    const id = e.currentTarget.dataset.item.id
    wx.navigateTo({
      url: `/pages/goods/itemWork/index?&id=${id}`,
    })
  },
  goToOrder() {
    wx.switchTab({
      url: '/pages/cart/index',
    })
  }
});